// 1.移除数组 [1, 2, 3, 4, 2] 中的2。不要直接修改数组，结果返回新的数组
let arr1 = [1, 2, 3, 4, 2];
let arr1Result = [];
function remove(arr, num) {
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] != num) {
      arr1Result.push(arr[i]);
    }
  }
  return arr1Result;
}
remove(arr1, 2);
console.log('arr1', arr1);
console.log('arr1Result', arr1Result);
// 2.定义一个函数,能在数组 [1, 2, 3, 4, 2] 的 "2"后面添加元素 "JavaScript"。不要直接修改数组 arr，结果返回新的数组
let arr2 = [1, 2, 3, 4, 2];
let arr2Result = [];
function add(arr, num) {
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] != num) {
      arr2Result.push(arr[i]);
    } else {
      arr2Result.push(arr[i]);
      arr2Result.push('JavaScript');
    }
  }
  return arr2Result;
}
add(arr2, 2);
console.log('arr2', arr2);
console.log('arr2Result', arr2Result);
// 3.统计数组 [1, 2, 3, 4, 2] 中2出现的次数
let arr3 = [1, 2, 3, 4, 2];
let count = 0;
function stat(arr, num) {
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] == num) {
      count++;
    }
  }
  return count;
}
stat(arr3, 2);
console.log('arr3', arr3);
console.log('2的出现次数是：', count);
// 4.找出数组 [1, 2, 3, "JavaScript", 4, "JavaScript", 2, 4, "JavaScript"] 中重复出现过的元素,并用数组将重复元素装起来输出
let arr4 = [1, 2, 3, 'JavaScript', 4, 'JavaScript', 2, 4, 'JavaScript'];
let arr4Result = [];
function repeat(arr) {
  arr.filter(function (item, index, self) {
    if (self.indexOf(item) !== index && !arr4Result.includes(item)) {
      arr4Result.push(item);
    }
  });
  return arr4Result;
}
repeat(arr4);
console.log('arr4', arr4);
console.log('arr4Result', arr4Result);
// 5.在数组里面输出年龄小于17的对象
let arr5 = [
  {
    name: '111',
    sex: 'boy',
    age: 18,
  },
  {
    name: '222',
    sex: 'girl',
    age: 17,
  },
  {
    name: '333',
    sex: 'boy',
    age: 16,
  },
  {
    name: '444',
    sex: 'girl',
    age: 15,
  },
  {
    name: '555',
    sex: 'boy',
    age: 20,
  },
];
let arr5Result = [];
function find(arr) {
  arr.filter(function (value, index, self) {
    if (self[index].age < 17) {
      arr5Result.push(value);
    }
    return arr5Result;
  });
}
find(arr5);
console.log('arr5', arr5);
console.log('arr5Result', arr5Result);
// 6. 数组扁平化, 把数组[1, [2, [3, 4, 5]]]转成[1, 2, 3, 4，5]
let arr6 = [1, [2, [3, 4, 5]]];
function flat(arr) {
  let arr6Flat = [];
  arr.forEach(function (item) {
    if (Array.isArray(item)) {
      arr6Flat = arr6Flat.concat(flat(item));
    } else {
      arr6Flat.push(item);
    }
  });
  return arr6Flat;
}
arr6Result = flat(arr6);
console.log('arr6', arr6);
console.log('arr6Result', arr6Result);
// 7. 使用reduce实现[1,2,3,4,9]数组中所有成员的和
let arr7 = [1, 2, 3, 4, 9];
let sum = arr7.reduce(function (a, b) {
  return a + b;
});
console.log(sum);
// 8. 数组去重的方法 (4种)
let arr8 = [1, 2, 3, 3, 4, 4, 5];
// 第一种
var arr8Result1 = arr8.filter(function (value, index, self) {
  return self.indexOf(value) === index;
});
console.log(arr8Result1);

// 第二种
var arr8Result2 = arr8.reduce(function (a, b) {
  if (!a.includes(b)) {
    a.push(b);
  }
  return a;
}, []);
console.log(arr8Result2);

// 第三种
var arr8Result3 = [];
arr8.forEach(function (value) {
  if (arr8Result3.indexOf(value) === -1) {
    arr8Result3.push(value);
  }
});
console.log(arr8Result3);

// 第四种
var arr8Result4 = [];
var obj = {};
for (var i = 0; i < arr8.length; i++) {
  if (!obj[arr8[i]]) {
    arr8Result4.push(arr8[i]);
    obj[arr8[i]] = true;
  }
}
console.log(arr8Result4);
